﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DSAServer.Models.Output;
using DSAServer.Service;
using DSAServer.Utils;
using Top.Api.Util;

namespace DSAServer.Controllers
{
    public class TopOAuthController : BaseController
    {
        public ActionResult Authorize()
        {
            ////return Content("授权码获取成功");
            string url = "https://oauth.taobao.com/token";
            Dictionary<string, string> props = new Dictionary<string, string>();
            props.Add("grant_type", "authorization_code");
            props.Add("code", "5TJTfANdf75HUj1nrJAUejfV13096");//this.Request["code"]);
            props.Add("client_id", "21363391");
            props.Add("client_secret", "a9b7ba70783b617e9998dc4dd82eb3c5");
            props.Add("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
            props.Add("view", "web");

            string s = "";
            try
            {
                WebUtils webUtils = new WebUtils();
                s = webUtils.DoPost(url, props);

                if (s != "")
                {
                    TaobaoAuthInfo tai = JsonHelper.DeserializeObject<TaobaoAuthInfo>(s);
                    ManagerService ms = new ManagerService();
                    List<ConfigItem> cis = new List<ConfigItem>();
                    cis.Add(new ConfigItem { ckey = "client_token", cvalue = tai.access_token });
                    ms.SaveConfigs(cis);
                }
            }
            catch (Exception e)
            { }

            //Process.Start("iexplore.exe", "https://oauth.taobao.com/token?grant_type=authorization_code&code=" + this.Request["code"] + "&client_id=23096885&client_secret=2450de9a4d7b29e60a8cbb43c0f83553&redirect_uri=http://www.mzc9.com/api/TopOAuth/Token&view=web");
            return Content("凭证获取成功");
        }
    }
}
